package com.bjesc_b2c.app.appraiset.mvp.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.ToDayPlanAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseFragment;
import com.bjesc_b2c.app.appraiset.http.IApiFragmentRequest;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.AddCarSourceActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.BasicInfoActivity;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.widget.SpringView;
import com.rxretrofit_build.util.MapUtil;

import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import butterknife.ButterKnife;
import butterknife.OnClick;
import master_bean.ToDayBean;
import rxretrofit_base.model.KeyBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;


public class FuturePlanFragment extends BaseFragment implements SpringView.OnFreshListener, MultiItemTypeAdapter.OnItemClickListener {

    RecyclerView recyclerView;
    SpringView springView;
    private ToDayPlanAdapter mToDayAdapter;
    private List<ToDayBean> data;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.fragment_todayplan, null);
        ButterKnife.inject(this, super.onCreateView(inflater, container, savedInstanceState));
        initViews();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    private void initViews() {

        recyclerView = (RecyclerView) contentView.findViewById(R.id.recycler_view);
        springView = (SpringView) contentView.findViewById(R.id.spring_view);
        springView.setListener(this);
        springView.setHeader(new DefaultFooter(getActivity()));
        springView.setFooter(new DefaultFooter(getActivity()));
        springView.setType(SpringView.Type.FOLLOW);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        getData();
    }


    private void getData() {
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(getActivity(), new MapUtil().build()).create(IApiFragmentRequest.class).today(),
                new BaseSubscriber<TokenBean<List<ToDayBean>>>(getActivity()) {
                    @Override
                    public void onSuccess(TokenBean<List<ToDayBean>> o) {
                        springView.onFinishFreshAndLoad();
                        if(o.getData()!=null && o.getData().size() >0){
                            refreshData(o.getData());
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        springView.onFinishFreshAndLoad();
                    }
                });
    }

    private void refreshData(List<ToDayBean> data) {
        this.data = data;
        if(mToDayAdapter == null){
            mToDayAdapter = new ToDayPlanAdapter(getActivity(), R.layout.layout_item_todayplan, data);
            mToDayAdapter.setOnItemClickListener(this);
            recyclerView.setAdapter(mToDayAdapter);
        }else{
            mToDayAdapter.refresh(data);
            mToDayAdapter.notifyDataSetChanged();
        }
    }

    @OnClick(R.id.tv_addCar)
    public void onClick(){
        startIntent(new Intent(getActivity(), AddCarSourceActivity.class));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    /**
     * @param view
     * @param holder
     * @param position
     */
    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {

        CustomSharedPreferences.saveString(getActivity(),new MapUtil().put(KeyBean.CARID.valueOf(),data.get(position).getId()).build(), KeyBean.FILE_NAME.valueOf());
        startIntent(new Intent(getActivity(), BasicInfoActivity.class));
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        getData();
    }

    /**
     * 上拉加载
     */
    @Override
    public void onLoadmore() {
        showToast("没有更多数据了");
        springView.onFinishFreshAndLoad();
    }

}
